package com.gopay.bis.kuaiqian.ebank.operation;

import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.kuaiqian.ebank.util.GatewayOrderQueryServiceLocator;
import com.gopay.bis.kuaiqian.ebank.util.MD5Util;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.kuaiqian.ebank.GatewayOrderDetail;
import com.gopay.common.domain.bank.kuaiqian.ebank.GatewayOrderQueryRequest;
import com.gopay.common.domain.bank.kuaiqian.ebank.GatewayOrderQueryResponse;
import com.gopay.common.domain.bank.kuaiqian.ebank.KuaiQianQueryMessage;
import com.gopay.common.domain.bank.kuaiqian.ebank.KuaiQianQueryResultMessage;

@Service("kuaiQianQueryHelper")
public class KuaiQianQueryHelper {


    private static final Logger logger = Logger.getLogger(KuaiQianQueryHelper.class);

    public GatewayOrderQueryResponse query(KuaiQianQueryMessage queryMessage) {

        logger.info("start to query 99bill order info ..........>> " + queryMessage);
        GatewayOrderQueryRequest queryRequest = new GatewayOrderQueryRequest();
        queryRequest.setInputCharset(queryMessage.getInputCharset());
        queryRequest.setVersion(queryMessage.getVersion());
        queryRequest.setSignType(Integer.parseInt(queryMessage.getSignType()));
        queryRequest.setMerchantAcctId(queryMessage.getMerchantAcctId());
        queryRequest.setQueryType(Integer.parseInt(queryMessage.getQueryType()));
        queryRequest.setQueryMode(Integer.parseInt(queryMessage.getQueryMode()));
        queryRequest.setOrderId(queryMessage.getBankOrderId());
        queryRequest.setStartTime(queryMessage.getStartTime());
        queryRequest.setEndTime(queryMessage.getEndTime());
        queryRequest.setRequestPage(queryMessage.getRequestPage());
        queryRequest.setSignMsg(queryMessage.getSignMsg());
        GatewayOrderQueryResponse queryResponse=null;
        logger.info("query request ..........>> " + queryRequest);
        BankLogUtils.printB2CQuerySendLog(queryMessage,queryMessage);
        GatewayOrderQueryServiceLocator locator = new GatewayOrderQueryServiceLocator();
        try {
            queryResponse = locator.getgatewayOrderQuery().gatewayOrderQuery(queryRequest);
            BaseMessage baseMsg = new BaseMessage();
            baseMsg.setBank(BankConstants.BANK_LOGO_KUAIQIAN);
            BankLogUtils.printB2CQueryBlackLog(baseMsg, queryResponse);
        } catch (RemoteException e) {
        	BankLogUtils.printB2CQueryExceptionLog(queryMessage, e);
        } catch (ServiceException e) {
            BankLogUtils.printB2CQueryExceptionLog(queryMessage, e);
        }
        return queryResponse;

    }

    public KuaiQianQueryResultMessage parse(GatewayOrderQueryResponse queryResponse) {

    	KuaiQianQueryResultMessage message = new KuaiQianQueryResultMessage();
        try {
            message.setVersion(queryResponse.getVersion());
            message.setSignType(String.valueOf(queryResponse.getSignType()));
            message.setMerchantAcctId(queryResponse.getMerchantAcctId());
            message.setErrCode(queryResponse.getErrCode());
            message.setCurrentPage(queryResponse.getCurrentPage());
            message.setPageCount(queryResponse.getPageCount());
            message.setPageSize(queryResponse.getPageSize());
            message.setRecordCount(queryResponse.getRecordCount());

            GatewayOrderDetail[] orders = queryResponse.getOrders();
            GatewayOrderDetail detail = orders[0];
            message.setOrderId(detail.getOrderId());
            message.setOrderAmount(String.valueOf(detail.getOrderAmount()));
            message.setOrderTime(detail.getDealTime());
            message.setPayResult(detail.getPayResult());
            message.setPayType(detail.getPayType());
            message.setPayAmount(String.valueOf(detail.getPayAmount()));
            message.setFee(String.valueOf(detail.getFee()));
            message.setDealId(detail.getDealId());
        } catch (Exception e) {
        	BaseMessage baseMsg = new BaseMessage();
        	baseMsg.setBank(BankConstants.BANK_LOGO_KUAIQIAN);
        	BankLogUtils.printB2CQueryExceptionLog(baseMsg, e);
        }
        return message;

    }

	public String signByMD5(String plainStr) {
		  String signStr ="";
	        try {
	           return MD5Util.md5Hex(((String)plainStr).getBytes("utf-8")).toUpperCase();
	        } catch (Exception e) {
	            logger.error(e.getMessage());
	        }
	        
	        return signStr;
	}
}
